package com.xiaoge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.entity.EduLecturer;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.service.OssService;
import com.xiaoge.vo.EduCourseVO;
import com.xiaoge.vo.EduLecturerVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.entity.EduCourse;
import com.xiaoge.mapper.EduCourseMapper;
import com.xiaoge.service.EduCourseService;
import org.springframework.web.multipart.MultipartFile;

/**
* @Author: 冷俊杰
* @Description: 课程管理
* @DateTime: 2022/10/28 10:29
**/
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService{
    @Resource
    private OssService ossService;

    @Override
    public Page<EduCourse> getEduCourseByPage(Page<EduCourse> page, String courseName) {
        List<EduCourse> eduCourseList = this.baseMapper.getEduCourseList(page, courseName);
        if (eduCourseList.size()==0){
            throw new BusinessException("暂无数据");
        }
        //将刚刚动态sql查询的内容给page对象,让它去实现分页
        page.setRecords(eduCourseList);
        return page;
    }

    @Override
    public void saveEduCourse(EduCourseVO eduCourse, MultipartFile file) {
        String lecturerName = eduCourse.getLecturerName();
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduCourse::getLecturerName,lecturerName);
        Long i = this.baseMapper.selectCount(wrapper);
        if(i!=0){
            throw new BusinessException("已有该课程");
        }
        eduCourse.setCourseLogo(ossService.upload(file));
        this.baseMapper.addEduCourse(eduCourse);
    }
}
